Page({
  data: {
    addressList: [], // 存储地址信息的变量
    defaultButtonColor: 'default',
    pang:false
  },

  onLoad: function (options) {
    // this.getAddressList();
    console.log('进入2')
    
    if (options !==undefined) {
      if (options.value.isDefault == 1) {
        console.log('进入3')
        this.setData({
          pang: true, 
        });
       
      }
    }
  },
  onShow: function () {
    // 页面显示时执行的逻辑
    this.getAddressList();
    // 执行相应的操作
   
  },

  getAddressList: function () {
    var that = this;
    var app = getApp();
    var wxUserId = app.globalData.userInfo.wxUserId;
    console.log(wxUserId)
    wx.request({
      url: `https://199shq.com/api/address/wxUser/${wxUserId}`,
      method: 'GET',
      header: {
        'Content-Type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        console.log(res.data);
        that.setData({
          addressList: res.data.data, // 设置数据到页面的 data 中
        });
        var pang = that.data.pang
        if (pang) {
          that.setDefaultAddresss()
        }
        console.log(that.data.addressList)
      },
      fail: function (res) {
        console.error('获取地址信息失败', res);
      },
    });
  },

  // 修改地址
  editAddress: function (event) {
    
    var index = event.currentTarget.dataset.index;
    var address = this.data.addressList[index];
    // 执行跳转逻辑或调用修改地址的接口
    var encodedAddress = encodeURIComponent(JSON.stringify(address));
    
    wx.navigateTo({
      url: `/pages/addressList/addressList?address=${encodedAddress}`
    });
    
  },

  // 删除地址
  deleteAddress: function (event) {
    var that = this;
    var addressId = event.currentTarget.dataset.addressId;
    console.log(addressId);

    wx.request({
      url: `https://199shq.com/api/address/delete/${addressId}`,
      method: 'PUT',
      header: {
        'Content-Type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        console.log(res); // 处理返回的数据

        // 刷新页面
        that.getAddressList();
      },
      fail: function (error) {
        console.log(error); // 请求失败的处理
      }
    });
  },
  // 设置默认地址
  setDefaultAddresss: function () {
    var that = this;
    var index = that.data.addressList.length - 1;
    console.log(that.data.addressList);
    console.log(that.data.addressList.length);
    console.log(index);
    var addressList = that.data.addressList;
    var addressId = addressList[index].addressId;
    var selectedAddress = addressList[index];
    console.log("设置默认地址："+addressId)

    wx.request({
      url: `https://199shq.com/api/address/isDefault/${addressId}`,
      method: 'PUT',
      header: {
        'Content-Type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        addressList.forEach(function (address, i) {
          if (i === index) {
            address.isDefault = 1;
            address.buttonColor = 'green';
          } else if (address.isDefault) {
            address.isDefault = 0;
            address.buttonColor = 'white';
          }
        });

        that.setData({
          addressList: addressList,
        });

        wx.showToast({
          title: '设置成功',
          icon: 'success',
          duration: 2000
        });
        wx.navigateBack({
          delta: 1
        });
      },
      fail: function (error) {
        console.log(error);
      }
    });
  },
  // 设置默认地址
  setDefaultAddress: function (event) {
    var that = this;
    var index = event.currentTarget.dataset.index;
    console.log(index);
    var addressList = that.data.addressList;
    var addressId = addressList[index].addressId;
    var selectedAddress = addressList[index];
    console.log("设置默认地址："+addressId)

    wx.request({
      url: `https://199shq.com/api/address/isDefault/${addressId}`,
      method: 'PUT',
      header: {
        'Content-Type': 'application/json',
        'token': wx.getStorageSync('token')
      },
      success: function (res) {
        addressList.forEach(function (address, i) {
          if (i === index) {
            address.isDefault = 1;
            address.buttonColor = 'green';
          } else if (address.isDefault) {
            address.isDefault = 0;
            address.buttonColor = 'white';
          }
        });

        that.setData({
          addressList: addressList,
        });

        wx.showToast({
          title: '设置成功',
          icon: 'success',
          duration: 2000
        });
        wx.navigateBack({
          delta: 1
        });
      },
      fail: function (error) {
        console.log(error);
      }
    });
  },

  chuancan: function () {
    wx.navigateTo({
      url: '/pages/addressList/addressList'
    });
  },
});